import time
from vec import *

WIDTH = 250
HEIGHT = 250

BACK_COLOR = (0, 0, 0)

unit = 50

a = [
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
]

class MyRect:
    def __init__(self, x, y, clr):
        self.pos = Vec2(x, y)
        self.clr = clr
        
    def Update(self):
        pass

    def Draw(self):
        rc = Rect((self.pos.x, self.pos.y), (unit, unit))
        screen.draw.filled_rect(rc, self.clr)

lastTime = time.time()
deltaTime = 0
spd = 5
next_pt_index = 1
p = 0
pspd = 0.01

road_point_list = []
unit = 50

rc = MyRect(0, 0, (100, 0, 0))

def move(start_pt, end_pt):
    global p, rc
    has_finished = False
    if p < 1.0:    
        p += pspd
    else:
        p = 1.0
        has_finished = True
    rc.pos = start_pt * unit * (1 - p) + end_pt * unit * p

    return has_finished

def reset():
    global road_point_list
    x = 0
    y = 0
    a[x][y] = 1
    road_point_list.append(Vec2(x, y))

    count = 25

    count -= 1
    while count > 0:
        while x + 1 < 5 and a[x + 1][y] == 0:
            x += 1
            a[x][y] = 1
            road_point_list.append(Vec2(x, y))
            count -= 1

        while y - 1 >= 0 and a[x][y - 1] == 0:
            y -= 1
            a[x][y] = 1
            road_point_list.append(Vec2(x, y))
            count -= 1

        while x - 1 >= 0 and a[x - 1][y] == 0:
            x -= 1
            a[x][y] = 1
            road_point_list.append(Vec2(x, y))
            count -= 1

        while y + 1 < 5 and a[x][y + 1] == 0:
            y += 1
            a[x][y] = 1
            road_point_list.append(Vec2(x, y))
            count -= 1

reset()

def on_key_down(key):
    if key == keys.ESCAPE:
        quit()
    elif key == keys.SPACE:
        reset()
        
def on_mouse_down(pos):
    print("mouse_down", pos)

def on_mouse_up(pos):
    print("mouse_up", pos)
    
def update():
    global next_pt_index, p
    if next_pt_index >= len(road_point_list):
        return

    start_pt = road_point_list[next_pt_index - 1]
    end_pt = road_point_list[next_pt_index]
    if move(start_pt, end_pt) == True:
        next_pt_index += 1
        p = 0

        
def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    rc.Draw()
